from selenium.webdriver.common.by import By


class Page(object):

    def __init__(self, driver, base_url):
        self.driver = driver
        self.base_url = base_url
        self.timeout = 20
        self.goto_page()
        # self.driver.switch_to.default_content()

    def goto_page(self):
        self.driver.get(self.base_url)

    def check_page_has_error(self):
        try:
            flag = self.find_element((By.XPATH, '//div[@role="alert"]')).is_enabled()
        except Exception as e:
            return True
        if flag:
            raise Exception("current page {} may has error".format(self.base_url))

    def find_element(self, *loc):
        return self.driver.find_element(*loc)

    def find_elements(self, *loc):
        """
        有的列表元素需要返回一个集合，像prechat的required字段
        :param loc:
        :return: webElement[]
        """
        return self.driver.find_elements(*loc)

    def input_text(self, loc, text):
        self.find_element(*loc).send_keys(text)

    def click(self, loc):
        self.find_element(*loc).click()

    def get_title(self):
        return self.driver.title
